/*
 * @Author: your name
 * @Date: 2021-11-22 16:02:33
 * @LastEditTime: 2022-01-06 21:14:29
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \agricultural-production-web\src\api\axios-online.js
 */
/*
*@descrion:此封装方法主要时用户的登录，与选择农场页面
*/
import axios from "axios";
import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import baseUrl from "@/api/baseURL"
const zhaxios = axios.create({
  // baseURL: "http://ia.senontech.com/",

  baseURL: baseUrl.webServiceUrl,
  // baseURL: "http://192.168.1.85:9090/production/",
  // baseURL: "http://192.168.1.97:9090/",
  timeout: 15000 // 请求超时时间
});

// request interceptor
zhaxios.interceptors.request.use(
  config => {
    config.headers["sysDataBaseName"] = "DB_TDW1_SC";
    if (getToken("token")) {
      config.headers["loginToken"] = getToken("token");
    }
    if (getToken("token")) {
      config.headers["systemMark"] = "SC";
      config.headers["databaseName"] = getToken("databaseName");
      config.headers["organizationList"] = getToken("organizationList");
      config.headers["organizationId"] = getToken("organizationId");
    }
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
zhaxios.interceptors.response.use(
  response => {
    const res = response.data;
    return res;
  },
  error => {
    console.log("err" + error);
    Message({
      message: error.message,
      type: "error",
      duration: 5 * 1000
    });
    return Promise.reject(error);
  }
);

// 封装get请求
export function get(url, params = {}) {
  params.t = new Date().getTime(); // get方法加一个时间参数,解决ie下可能缓存问题.
  return zhaxios({
    url: url,
    method: "get",
    params
  });
}

// 封装post请求
export function post(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: "post",
    data
  };
  sendObject.data = JSON.stringify(data);
  return zhaxios(sendObject);
}

export default zhaxios;
